package com.tiger.admin.http.deptContacts;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.tiger.admin.Db;
import com.tiger.db.entity.DeptContacts;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@Service
public class DeptContactsService {
    private final Db db;

    public DeptContactsService(Db db) {
        this.db = db;
    }

    public List<DeptContacts> list() {
        return db.deptContacts.list();
    }


    public void importContacts(List<HashMap<String, Object>> rows) {
        for(HashMap<String, Object> row : rows) {
            System.out.println("ccccc");
            System.out.println(row);
            System.out.println("pppppppppppp");

            String dept = (String) row.get("dept");
            DeptContacts deptContacts = db.deptContacts.getOne(new LambdaQueryWrapper<DeptContacts>().eq(DeptContacts::getDept, dept));


            if(deptContacts == null) {
                deptContacts = new DeptContacts();
                deptContacts.setDept(dept);
                deptContacts.setDepts((List<HashMap<String, Object>>) row.get("depts"));

                System.out.println(db.deptContacts.save(deptContacts));
                continue;
            }

            deptContacts.setDepts((List<HashMap<String, Object>>) row.get("depts"));
            db.deptContacts.updateById(deptContacts);
        }
    }

    public void delContacts(Integer id, Integer index, Integer i) {
        DeptContacts deptContacts = db.deptContacts.getOne(new LambdaQueryWrapper<DeptContacts>().eq(DeptContacts::getId, id));
        if(deptContacts == null) return;

        List<HashMap<String, Object>> depts = deptContacts.getDepts();
        if(depts == null || depts.isEmpty()) {
            db.deptContacts.removeById(new LambdaQueryWrapper<DeptContacts>().eq(DeptContacts::getId, id));
            return;
        }

        HashMap<String, Object> dept = depts.get(index);
        if (dept == null) {
            System.out.println("Invalid index for depts");
            return;
        }

        List<HashMap<String, Object>> contacts = (List<HashMap<String, Object>>) dept.get("contacts");
        if (contacts == null || contacts.isEmpty()) {
            depts.remove(dept.get(index));
            System.out.println("Contacts list is null");
            return;
        }

        if (i < 0 || i >= contacts.size()) {
            System.out.println("Invalid index for contacts");
            return;
        }

        HashMap<String, Object> contactToRemove = contacts.get(i);
        boolean isRemoved = contacts.remove(contactToRemove);
        System.out.println("Is contact removed: " + isRemoved);
        System.out.println("Updated contacts list: " + contacts);

        System.out.println("Contacts list is empty11111");
        System.out.println(contacts);
        System.out.println(contacts.isEmpty());

        if(contacts.isEmpty()) {
            System.out.println("Contacts list is empty");
            System.out.println(depts.get(index));

            depts.remove(depts.get(index));
        }

        System.out.println(depts.isEmpty());

        System.out.println(deptContacts);

        if(depts.isEmpty()) {
            System.out.println("直接删除");


            boolean r = db.deptContacts.removeById(id);
            System.out.println(r);
            return;
        }

        deptContacts.setDepts(depts);

        System.out.println(deptContacts);


        db.deptContacts.updateById(deptContacts);
    }


    public void importContacts(MultipartFile file) {
        try (Workbook workbook = new XSSFWorkbook(file.getInputStream())) {
            for (int i = 0; i < workbook.getNumberOfSheets(); i++) {
                Sheet sheet = workbook.getSheetAt(i);
                readSheet(sheet);
            }
        }catch (Exception e) {
            System.out.println(e.getMessage());
        }
    }

    private void readSheet(Sheet sheet) {
//        String name = sheet.getSheetName();
//
//        List<HashMap<String, String>> contacts = new ArrayList<>();
//
//        for (Row row : sheet) {
//            if(row.getRowNum() == 0) continue;
//
//            Cell cell0 = row.getCell(0);
//            if(cell0 == null || cell0.getStringCellValue().isEmpty()) continue;
//
//            Cell cell1 = row.getCell(1);
//            if(cell1 == null || cell1.getStringCellValue().isEmpty()) continue;
//
//
//
//            HashMap<String, String> item = new HashMap<>();
//            item.put("dept", cell0.getStringCellValue());
//            item.put("tel", cell1.getStringCellValue());
//
//            Cell cell2 = row.getCell(2);
//            if(cell2 != null && !cell2.getStringCellValue().isEmpty()) {
//                item.put("remark", cell2.getStringCellValue());
//            }
//
//            contacts.add(item);
//        }
//
//        if(contacts.isEmpty()) return;
//
//        DeptContacts deptContacts = db.deptContacts.getOne(new LambdaQueryWrapper<DeptContacts>().eq(DeptContacts::getName, name));
//
//
//        if(deptContacts == null) {
//            deptContacts = new DeptContacts();
//            deptContacts.setName(name);
//            deptContacts.setContacts(contacts);
//
//            System.out.println(db.deptContacts.save(deptContacts));
//            return;
//        }
//
//        deptContacts.setContacts(contacts);
//        db.deptContacts.updateById(deptContacts);

    }
}
